﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using EquipSecs300;
using EquipSecs300.Parameter;
using Newtonsoft.Json;
using SecsEquip;
using SecsEquip300;

namespace EquipSecs300
{
    public partial class LoadPortSubView : CMyFormEx
    {
        int m_nPortNo = 0;
        public GEM_LoadPortSub GEMLoadPortSub { get; set; }

        public LoadPortSubView()
        {
            m_pViewName = "Carrier";

            InitializeComponent();
        }

        // 设置端口号
        public void SetPortNo(int nPortNo)
        {
            m_nPortNo = nPortNo;

            InitListView();
            InitDataGridView();
        }

        public int GetPortNo()
        {
            return m_nPortNo;
        }
        public override void UpdateData()
        {
            UpdateGrid();
        }

        public void OnCarrierInfoChange()
        {
            if (!this.Created) return;

            if (InvokeRequired)
            {
                BeginInvoke(new Action(UpdateGrid));
            }
            else { UpdateGrid(); }

        }
        private void UpdateGrid()
        {
            if (GEMLoadPortSub.MapSlotResult != null && GEMLoadPortSub.MapSlotResult.Count != 0)
            {
                rowsQty = GEMLoadPortSub.MapSlotResult.Count;
                InitDataGridView();
            }
            UpdataGridView(GEMLoadPortSub.LoadPortBase.PortInfo[m_nPortNo]);
            UpdataListView(GEMLoadPortSub.LoadPortBase.PortInfo[m_nPortNo]);
            UpdataPropertyGrid(GEMLoadPortSub.LoadPortBase.PortInfo[m_nPortNo]);

        }

        int timecot = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            textBoxTransferState.Text = GEMLoadPortSub.loadPortTransferState_KE.ToString();
            textBoxCarrierIDStatus.Text = GEMLoadPortSub.carrierIDStatus_KE.ToString();
            textBoxCarrierSlotStatus.Text = GEMLoadPortSub.carrierSlotMapStatus_KE.ToString();

        }

        // 其他内容处理

        // 准备上料、下料
        private void btnReadyLoadUnload_Click(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierInLoadPort.Checked;
            if (bCheck)
            {
                // 有料
                GEMLoadPortSub.Ev_LoadPortUnloadReady_R();
                GEMLoadPortSub.Ev_LoadPortUnloadReady(PortFoupStatus_KE.UnLoaded, GEMLoadPortSub.CarrierID);
            }
            else
            {
                // 没有料
                GEMLoadPortSub.Ev_LoadPortLoadReady_R();
                GEMLoadPortSub.Ev_LoadPortLoadReady(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
            }
        }

        // 放有料在Load Port
        private void checkBoxCarrierInLoadPort_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierInLoadPort.Checked;
            if (bCheck)
            {
                // 上料过程		
                GEMLoadPortSub.Ev_LoadPortLoading_R();
                GEMLoadPortSub.Ev_LoadPortLoading();

                // 上料完成
                GEMLoadPortSub.Ev_LoadPortLoaded_R();
                GEMLoadPortSub.Ev_LoadPortLoaded(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
            }
            else
            {
                // 下料过程
                GEMLoadPortSub.Ev_LoadPortUnLoading_R();
                GEMLoadPortSub.Ev_LoadPortUnLoading();
                // 下料完成
                GEMLoadPortSub.Ev_LoadPortUnLoaded_R();
                GEMLoadPortSub.Ev_LoadPortUnLoaded(PortFoupStatus_KE.UnLoaded, GEMLoadPortSub.CarrierID);
                //	m_pSecs->CmsLoadPortUnbind(m_nPortNo);	

            }
        }

        private void btnLoaded_Click(object sender, EventArgs e)
        {
            // 上料完成
            GEMLoadPortSub.Ev_LoadPortLoaded_R();
            GEMLoadPortSub.Ev_LoadPortLoaded(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
        }

        private void btnUnloaded_Click(object sender, EventArgs e)
        {
            // 下料完成
            GEMLoadPortSub.Ev_LoadPortUnLoaded_R();
            GEMLoadPortSub.Ev_LoadPortUnLoaded(PortFoupStatus_KE.UnLoaded, GEMLoadPortSub.CarrierID);
        }

        // Load Port是否锁紧料盒
        private void checkBoxCarrierClamp_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierClamp.Checked;
            if (bCheck)
            {
                // 锁紧
                GEMLoadPortSub.Ev_BoxCarrierClamped_R();
                GEMLoadPortSub.Ev_BoxCarrierClamped(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
            }
            else
            {
                // 解锁
                GEMLoadPortSub.Ev_BoxCarrierUnClamped_R();
                GEMLoadPortSub.Ev_BoxCarrierUnClamped(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
            }
        }

        // 料盒CmsLoadPortDocked
        private void BoxCarrierDocked_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = BoxCarrierDocked.Checked;
            if (bCheck)
            {
                GEMLoadPortSub.Ev_BoxCarrierDocked_R();
                GEMLoadPortSub.Ev_BoxCarrierDocked();
            }
            else
            {
                GEMLoadPortSub.Ev_BoxCarrierUnDocked_R();
                GEMLoadPortSub.Ev_BoxCarrierUnDocked();
            }
        }

        // 料盒门打开情况
        private void checkBoxCarrierOpen_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierOpen.Checked;
            if (bCheck)
            {
                // 打开
                GEMLoadPortSub.Ev_BoxCarrierOpened_R();
                GEMLoadPortSub.Ev_BoxCarrierOpened(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
            }
            else
            {
                // 关闭
                GEMLoadPortSub.Ev_BoxCarrierClosed_R();
                GEMLoadPortSub.Ev_BoxCarrierClosed(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
            }
        }

        private void LoadPortSubView_Load(object sender, EventArgs e)
        {

        }

        // 读取料盒ID成功
        private void BtnReadCarrierSucc_Click(object sender, EventArgs e)
        {
            string pID = textBoxCarrierID.Text;
            if (pID == null ||
                pID.Length == 0)
            {
                MessageBox.Show("Carrier ID为空");
            }
            else
            {
                GEMLoadPortSub.Ev_ReadCarrierSucc_R();
                GEMLoadPortSub.Ev_ReadCarrierSucc(PortFoupStatus_KE.Loaded, pID);
            }
        }

        // 读取料盒ID失败
        private void BtnReadCarrierFail_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_ReadCarrierFail_R();
            GEMLoadPortSub.Ev_ReadCarrierFail(PortFoupStatus_KE.Loaded, GEMLoadPortSub.CarrierID);
        }

        // 读取Slot成功
        private void BtnReadCarrierSlotSucc_Click(object sender, EventArgs e)
        {
            string pID = textBoxCarrierID.Text;
            if (pID == null ||
                pID.Length == 0)
            {
                MessageBox.Show("Carrier ID为空");
            }
            else
            {
                List<ushort> list = new List<ushort>();
                for (int i = 0; i < UtilitiesConvert.StringToInt32(textBox2.Text); i++)
                {
                    if (i > 2 && i < 6)
                    {
                        list.Add(1);
                    }
                    else
                    {
                        list.Add(0);
                    }
                }

                GEMLoadPortSub.Ev_ReadCarrierSlotSucc_R();
                GEMLoadPortSub.Ev_ReadCarrierSlotSucc(PortFoupStatus_KE.Loaded, pID, list);
            }
        }

        private void btnProcessStar_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_BoxCarrierProcessStart_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessStart(PortFoupStatus_KE.FoupWorking, textBoxCarrierID.Text);
        }

        private void btnProcessEnd_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_BoxCarrierProcessEnd_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessEnd(PortFoupStatus_KE.FoupWorking, textBoxCarrierID.Text);
        }

        private void btnVerifyCarrier_2_Click(object sender, EventArgs e)
        {
            string sRes = GEMLoadPortSub.VerifyCarrierMapRes(new List<ushort>());

            int nRes = UtilitiesConvert.StringToInt32(textBoxVerifyMapRes.Text);
            GEMLoadPortSub.Ev_VerifyCarrierMap_2_R();
            GEMLoadPortSub.Ev_VerifyCarrierMapRes_2(nRes.ToString(), PortFoupStatus_KE.FoupWorking, textBoxCarrierID.Text);
        }

        string _CJobID = "";
        string _PJobID = "";
        private void getPJobInfo_Click(object sender, EventArgs e)
        {
            string recipeID;
            List<int> lotListInt;
            List<bool> lotListBool;
            StringBuilder sb = new StringBuilder();
            string cJobID = secsEquipHelper.CJob.GetUseAbleCJobID();
            secsEquipHelper.PJob.GetPJobListByCJobID(cJobID);
            string pJobID = secsEquipHelper.PJob.GetUseAblePJobID(cJobID, textBoxCarrierID.Text);
            secsEquipHelper.PJob.GetPJobInfo(pJobID, textCarrierID.Text, out recipeID, out lotListInt);
            _CJobID = cJobID;
            _PJobID = pJobID;

            if (recipeID == null || lotListInt == null)
            {
                lblRecipeID.Text = "recipeID：null";
                lblSlotNO.Text = "slotNO：null";
                return;
            }

            for (int i = 0; i < lotListInt.Count; i++)
            {
                if (i != lotListInt.Count - 1)
                {
                    sb.Append(lotListInt[i] + ",");
                }
                else
                {
                    sb.Append(lotListInt[i]);
                }
            }

            lblRecipeID.Text = "recipeID：" + recipeID;
            lblSlotNO.Text = "slotNO：" + sb.ToString();

        }

        private void btnIsPJobByCurCJobComplete_Click(object sender, EventArgs e)
        {
            bool isPJobComplete = false;
            isPJobComplete = secsEquipHelper.PJob.IsPJobByCurCJobComplete(_CJobID, textCarrierID.Text);
            if (isPJobComplete)
            {
                lblIsPJobComplete.Text = "true";
            }
            else
            {
                lblIsPJobComplete.Text = "false";
            }
        }

        private void btnRecipeChanged_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_RecipeChangeSucc_R();
            GEMLoadPortSub.Ev_RecipeChangeSucc("8-XAC64K15B", "lot001", null);
        }

        private int rowsQty = 25;
        string slotName = "";
        private void InitDataGridView()
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Rows.Add(rowsQty);

            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                slotName = (rowsQty - item.Index).ToString("D2");
                item.Height = 30;//(int)(dataGridView1.Size.Height / (rowsQty + 1));
                item.Cells[0].Value = slotName;
                item.Cells[0].Style.ForeColor = Color.Black;
                item.Cells[0].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }

            dataGridView1.ClearSelection();
        }
        private void UpdataGridView(PortInfo portInfo)
        {
            if (portInfo == null || portInfo.CarrierInfo == null)
            {
                for (int i = 1; i < rowsQty; i++)
                {
                    dataGridView1.Rows[rowsQty - i].Cells[1].Value = "";
                    dataGridView1.Rows[rowsQty - i].DefaultCellStyle.BackColor = Color.White;
                }
                return;
            }

            for (int i = 0; i < rowsQty; i++)
            {
                for (int j = 1; j < rowsQty; j++)
                {
                    if (portInfo.CarrierInfo.pSlotData[i].nSlot == j)
                    {
                        dataGridView1.Rows[rowsQty - j].Cells[1].Value = portInfo.CarrierInfo.pSlotData[i].pSubstrateID;

                        switch (portInfo.CarrierInfo.pSlotData[i].nSlotMapEnum)
                        {
                            case SlotMap.CORRECTLY_OCCUPIED:
                                {
                                    dataGridView1.Rows[rowsQty - j].DefaultCellStyle.BackColor = Color.LightGreen;
                                }
                                break;
                            case SlotMap.DOUBLESLOTTED:
                                {
                                    dataGridView1.Rows[rowsQty - j].DefaultCellStyle.BackColor = Color.Red;
                                }
                                break;
                            default:
                                {
                                    dataGridView1.Rows[rowsQty - j].DefaultCellStyle.BackColor = Color.White;
                                }
                                break;
                        }
                    }
                }

            }
        }
        private void InitListView()
        {
            listViewCarrier.Columns.Add("项目", 200, HorizontalAlignment.Left);
            listViewCarrier.Columns.Add("值", 300, HorizontalAlignment.Left);
        }
        private void UpdataListView(PortInfo portInfo)
        {
            listViewCarrier.Items.Clear();

            if (portInfo == null || portInfo.CarrierInfo == null)
            {
                return;
            }

            listViewCarrier.BeginUpdate();

            ListViewItem lvi = new ListViewItem();
            lvi.Text = "CarrierID";
            lvi.SubItems.Add(portInfo.CarrierInfo.pCarrierID);
            listViewCarrier.Items.Add(lvi);

            lvi = new ListViewItem();
            lvi.Text = "CarrierIDStatus";
            lvi.SubItems.Add(portInfo.CarrierInfo.nCarrierIDStatus.ToString());
            listViewCarrier.Items.Add(lvi);

            lvi = new ListViewItem();
            lvi.Text = "CarrierAccessingStatus";
            lvi.SubItems.Add(portInfo.CarrierInfo.nCarrierAccessingStatus.ToString());
            listViewCarrier.Items.Add(lvi);

            lvi = new ListViewItem();
            lvi.Text = "SlotMapStatus";
            lvi.SubItems.Add(portInfo.CarrierInfo.nSlotMapStatus.ToString());
            listViewCarrier.Items.Add(lvi);

            listViewCarrier.EndUpdate();
        }
        private void UpdataPropertyGrid(PortInfo portInfo)
        {
            var carrierInfo = UtilitiesFile.DeepCopy2<Carrier, CarrierInfo>(portInfo.CarrierInfo);
            propertyGrid1.SelectedObject = carrierInfo;
            propertyGrid1.Refresh();
        }

    }
}
